package com.boot.controller;

import com.boot.bean.RoomDo;
import com.boot.bean.UserDo;
import com.boot.enums.ResultEnum;
import com.boot.service.RoomService;
import com.boot.service.UserService;
import com.boot.vo.LayuiModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@RequestMapping("/room")
public class RoomController {

    @Autowired
    private RoomService roomService;

    @Autowired
    private UserService userService;

    @RequestMapping("/list")
    public Object list(Integer page, Integer limit, HttpServletRequest request) {
        String id = request.getParameter("id");
        if (StringUtils.isEmpty(id)) {
            return new LayuiModel().setMsg("游戏类型为空");
        }
        RoomDo model = new RoomDo();
        model.setGameId(Long.valueOf(id));
        Page<RoomDo> pageer = roomService.findAll(model, page-1, limit);
        return new LayuiModel().setCode(0L).setCount(pageer.getTotalElements()).setData(pageer.getContent());
    }

    @RequestMapping("/addOrUpdate")
    public Object add(RoomDo model,HttpServletRequest request) {
        UserDo attribute = (UserDo)request.getSession().getAttribute(ResultEnum.USERKEY.getMessage());
        if(attribute ==null){
            return  new LayuiModel(ResultEnum.NOTLOGIN);
        }
        UserDo one = userService.getOne(attribute.getUserId());
        if(one ==null){
            return  new LayuiModel(ResultEnum.NOTFINDUSER);
        }
        model.setCreateDate(new Date());
        model.setUserName(one.getLoginName());
        RoomDo save = roomService.save(model);
        return new LayuiModel().setCode(0L).setData(save);
    }
}
